package pers.qianyu.month_202012.date_20201210;

import org.junit.*;

import java.util.*;

/**
 * 90. 子集 II
 * https://leetcode-cn.com/problems/subsets-ii/
 *
 * @author mizzle rain
 * @date 2020-12-10 17:52
 */
public class SubsetsWithDup {
    /**
     * 直接迭代
     */
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        if (nums == null || nums.length == 0) {
            return new ArrayList<>();
        }
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        int pre = 0;
        res.add(new ArrayList<>());
        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            int size = res.size();
            int start = 0;
            if (i != 0 && nums[i - 1] == num) {
                start = pre;
            }
            for (int j = start; j < size; j++) {
                List<Integer> list = new ArrayList<>(res.get(j));
                list.add(num);
                res.add(list);
            }
            pre = size;
        }
        return res;
    }

    @Test
    public void test1() {
        int[] param = {1, 2, 2};
        List<List<Integer>> result = new SubsetsWithDup().subsetsWithDup(param);
        result.forEach(System.out::println);
    }
}
